<template>
	<view class="page-content">
		<text class="btn btn-main btn-xs btn-radius" @click="showVoiceAd">{{btn}}</text>
	</view>
</template>

<script>
	const voicdeReadSdk = uni.requireNativePlugin('xlx-voice-read-module');
	export default {
		name:"m-voice-reward",
		props: ['btn','seatid'],
		data() {
			return {
				setting:[],
				userInfo:[]
			};
		},
		mounted(){
			this.setting = uni.getStorageSync('_config');

			this.userInfo = uni.getStorageSync('userInfo');
			// const voicdeReadSdk = uni.requireNativePlugin('xlx-voice-read-module');
			
			// 初始化语音插件
			voicdeReadSdk.init({
			    appId: this.setting.xlx_appid,//渠道appId，从配置后台获取
			    appSecret: this.setting.xlx_secret, //密钥，从配置后台获取
			    debug: true 
			});
			this.loadVoiceAd();
			// const test = '123';
			// this.cesjh()
			// if(this.seatid){
			// 	this.loadVoiceAd();
			// }else{
			// 	this.btn = '资源位ID错误';
			// }
		},
		methods: {
			// cesjh(){
			// 	alert(test);
			// },
			// 初始化语音红包插件
			voiceInit(){
				
			},
			loadVoiceAd(){
				console.log(voicdeReadSdk);
				voicdeReadSdk.loadVoiceAd({
				    resourceId: this.seatid, //必要，资源位id
				    userId: this.userInfo['uid'], //可选，用于服务端回调时通知接入方给哪个用户发放奖励，不走服务端回调非必要
				    mediaExtra: "", //可选，额外的透传参数，仅支持单个json对象格式，不可以嵌套json对象
				    nickname: "" //可选，用户昵称
				}, 
				result => {
				    console.log("result = " + JSON.stringify(result));
					switch (result.eventType) {
						//广告加载成功
						case "onAdLoadSuccess":
							// eCPM 千次展现预估收益，单次展现收益需要除1000
							this.eCPM = result.params.eCPM;
							//第一阶段奖励iCPM
							this.iCPMOne = result.params.iCPMOne;
							//第二阶段奖励iCPM
							this.iCPMTwo = result.params.iCPMTwo;
							//每天最大朗读次数
							var maxReadNum = result.params.maxReadNum;
							 //每天剩余朗读次数
							var surplusReadNum = result.params.surplusReadNum;
							break;
						// 广告加载失败
						case "onAdLoadError":
							var errorCode = result.params.errorCode;
							var errorMsg = result.params.errorMsg;
							break;
					} 
				})
			},
			showVoiceAd(){
				voicdeReadSdk.showVoiceAd({
					/* rewardName 媒体给用户发放的奖励名称
					设置给用户发放的奖励值  需要开发者根据APP内虚拟币兑换比例来进行换算。比如:我方返回iCPM为100，表示此次千次互动的收益为100元，单次互动收益为0.1元。如媒体的虚拟币与人民币的兑换比例为1:10000，此次媒体的收益为1000，如果媒体给用户的分成比例为0.6，则此次应该返回600，SDK界面给用户展示为600金币
					rewardCount(iCPM/1000*虚拟币的兑换比例*给用户的分成比例); */
					"rewardInfo": [
						{"rewardCount": this.iCPMOne * 10, "rewardName": "金币"}, 
						{"rewardCount": this.iCPMTwo * 10, "rewardName": "金币"}
					],
				}, result => {
					//console.log("result = " + JSON.stringify(result));
					switch (result.eventType) {
						case "onAdShow":
							//语音展示完成
							break;
						case "onAdError":
							var errorCode = result.params.errorCode;
							//广告加载失败或者用户朗读失败
							break;
						case "onAdClose":
							//广告关闭
							break;
						/**
						* 奖励验证回调，开发者在需要在此回调中做奖励的发放
						* 如果对接多接奖励功能，那么此回调可能会执行多次
						*/
						case "onRewardVerify":
							// logId 奖励唯一标识
							var logId = result.params.logId;
							// iCPM 千次展现实时收益，单次展现收益需要除1000
							var iCPM = result.params.iCPM;
							//表示第几阶奖励
							var stepNum = result.params.stepNum;
							break;
					} 
				})
			},
			// init(){
			// 	const voicdeReadSdk = uni.requireNativePlugin('xlx-voice-read-module');
			// 	voicdeReadSdk.init({
			// 	    appId: this.appId,//渠道appId，从配置后台获取
			// 	    appSecret: this.appSecret, //密钥，从配置后台获取
			// 	    debug: false
			// 	});
			// }
		}
	}
</script>

<style lang="scss">
	.btn{
		border: 2px solid $maincolor-2;
		background-color: #fff!important;
		color: $maincolor-2;
		font-weight: bold;
	}
</style>